package org.mathcloud.math.core.generic.operator.algebra;


import org.mathcloud.math.core.common.operator.algebra.Plus;
import org.mathcloud.math.core.common.quantity.ComplexNumber;
import org.mathcloud.math.core.common.quantity.QuantityObject;
import org.mathcloud.math.core.common.quantity.RealNumber;
import org.mathcloud.math.core.common.signature.QuantityObjectSignature;
import org.mathcloud.math.core.generic.operator.GenericNaryOperator;
import org.mathcloud.math.core.generic.quantity.GenericComplexNumber;
import org.mathcloud.math.core.generic.quantity.GenericRealNumber;


public class GenericPlus extends GenericNaryOperator implements Plus
{
	public GenericPlus()
	{
		super();
		signature.setMMLContentTag("plus");
	}

	@Override	
	public QuantityObject calculate(QuantityObject operand1, QuantityObject operand2)
	{
		QuantityObjectSignature resSign = getResultSignature(operand1, operand2); 
		if (resSign!= null)
		{
			if (resSign.isRealNumberSignature())
			{
				return new GenericRealNumber(((RealNumber)operand1).getValue()+((RealNumber)operand2).getValue());
			}
			else if (resSign.isComplexNumberSignature())
			{
	      double real, image;
	      real = image = 0;
	      if (operand1.getSignature().isRealNumberSignature())
	      {
	        real = ((RealNumber)operand1).getValue();
	      } else if (operand1.getSignature().isComplexNumberSignature())
	      {
	        real = ((ComplexNumber)operand1).getRealPart();
	        image = ((ComplexNumber)operand1).getImagePart();
	      }
	      if (operand2.getSignature().isRealNumberSignature())
	      {
	        real = real + ((RealNumber)operand2).getValue();
	      } else if (operand2.getSignature().isComplexNumberSignature())
	      {
	        real = real + ((ComplexNumber)operand2).getRealPart();
	        image = image + ((ComplexNumber)operand2).getImagePart();
	      }
	      return new GenericComplexNumber(real,image);														
			}
		}
		return null;
	}
}
